<?php
// +----------------------------------------------------------------------
// | EtrolPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Author: Author: LJQ <245369209@qq.com> 
// +----------------------------------------------------------------------

namespace app\admin\controller\api;
use app\common\controller\Api;
/**
 * 用户管理
 * @package app\enterprise
 */
class User extends Api

{

    /**
   * 用户列表
   * @param string userId 用户id,管理员显示所有用户id,.
   * @return  json 
   * 
   *
   */

	public function userList($userId = 1){
            $model=model('common/User');
             // $data['enterpriseName'] = $user->enterprise[0]->fullName;
             // return json($user);
             // $enterpriseId=model('common/User')->where(array('id'=>$userId))->select();
             // $enterprise=$enterpriseId[0]['enterpriseId'];
             $where = [] ;
              if(($limit = input('limit')) && ($page = input('page'))){
               $startLimit =  ($page-1)*$limit;

               $info = collection(model('common/User')->with('roles,enterprise')->where($where)->limit($startLimit.','.$limit)->select())->toArray();
             }else{
               $info = collection(model('common/User')->with('roles,enterprise')->select())->toArray();
             }

		// if($enterprise==0){
               //管理员用户列表
               
               $count = count($info); 
               
               return json(['code'=>1,'msg'=>'用户信息','count'=>$count,'data'=>$info]);
             
             // }
             // if ($enterprise!=0) {
             //       $where['enterpriseId']=['eq',$enterprise];
             //    //非管理员用户列表
             //      $info =collection(model('common/User')->where($where)->select())->toArray();
             //      $count = count($info); 
             //      return json(['code'=>1,'msg'=>'用户信息','count'=>$count,'data'=>$info]);
             //    }
 	 }
   	  
   	      //添加用户
   	 public function add($data){
           if (empty($data)) {
               return json(['code'=>0,'msg'=>'数据异常,请检查']);
           }
           $roleId = $data['role'];
           unset($data['role']);
           $model = model('common/User');
           if(false == $model->save($data)){
           		 return json(['code'=>0,'msg'=>'添加失败,请检查']);
           }else{
           		$user = $model->get(6);
           		if($user->roles()->attach($roleId)){
                      setlog('Useradd','User',session('user_auth.id'));
           			return json(['code'=>1,'msg'=>'用户添加成功']);
           		}
           }
           return json(['code'=>0,'msg'=>'添加失败,请检查']);
   	 }
   	
         /**
   * 修改用户
   * @param string userId 用户id,
   * @return json
   *
   */
   	public function edit($data = null,$userId = null){
        
          if (empty($userId)) {
              return json(['code'=>0,'msg'=>'用户不存在,请检查']);
          }
          if ($userId == 1) {
              return json(['code'=>0,'msg'=>'admin账号不能被修改']);
          }
		
          $model = model('common/User');
    	  $user = $model->get($userId);
    	  //解除原有角色管理
    	  $user->roles()->detach();
    	  $roleId = $data['role'];
    	  unset($data['role']);
    	  //管理新角色
    	  if($user->roles()->attach($roleId)){
	          if ($model->change($data)) {
               setlog('Userupdate','User',session('user_auth.id'));
	             return json(['code'=>1,'msg'=>'用户修改成功']);
	            }else{
	             return json(['code'=>0,'msg'=>'用户修改失败,请检查']);
	            }
	       }
    	  return json(['code'=>0,'msg'=>'用户修改失败,请检查']);
   }

   	 
     
    /**
   *  删除用户
   * @param  string userId 用户id,
   * @return  json
   *
   */
     
    public function del($userId) {
            $userId = input('userId',0,'intval');
            $model =  model('common/User');
        if ($userId==1) {
            return  json(['code'=>0,'msg'=>'最高管理员不能被删除']);
        }


        if (empty ( $userId )||empty(($model->where(array('id'=>$userId))->find()))) {
           return  json(['code'=>0,'msg'=>'用户id不存在,请检查']);
         }
          
        if($model->destroy(['id'=>$userId]) !== FALSE){
         db('roleaccess')->where(array('userId'=>$userId))->delete();
          setlog('Userdel','User',session('user_auth.id'));
         return json(['code'=>1,'msg'=>'用户删除成功']);
       }else{
          return $this->json(['code'=>0,'msg'=>'用户删除失败,请检查']);
       }
    }

     
   /**
   *   查询用户
   * @param string  userName用户名称
   * @return  json
   *
   */
   	public function search($keyword=null){
   		 if($keyword){
                        if(($limit = input('limit')) && ($page = input('page'))){
                        $startLimit =  ($page-1)*$limit;
                        $info =collection(model('common/User')->with('roles,enterprise')->where('userName','like','%'.$keyword.'%')->limit($startLimit.','.$limit)->select())->toArray();
                         }else{
                        $info =collection(model('common/User')->with('roles,enterprise')->where('userName','like','%'.$keyword.'%')->select())->toArray();
                      }
             }else{
                     if(($limit = input('limit')) && ($page = input('page'))){
                       $startLimit =  ($page-1)*$limit;
                       $info =collection(model('common/User')->with('roles,enterprise')->limit($startLimit.','.$limit)->select())->toArray();
                     }else{
                       $info =collection(model('common/User')->with('roles,enterprise')->select())->toArray();
                     }
         }
	 	//总数
	      $count = count($info); 
		  if (empty($info) && $count == 0) {
          return json(['code'=>0,'msg'=>'没有找到用户','count'=>$count]);
       	} 
       	
       	if ($count > 0){
       		return json(['code'=>1,'msg'=>'用户信息','count'=>$count,'data'=>$info]);
       	}
      }


    /**
   *  角色列表
   * @param  
   * @return  json
   *
   */
     
    public function getRole() {
            $role=model('common/Role');
              //角色列表
            $roles =$role->field('id,roleName')->order(array('id' => 'DESC'))->select();
    		return json(['code'=>1,'msg'=>'角色列表','data'=>$roles]);
    }
	
    
    public function getEnterprise() {
    	$enterprise = model('basic/Enterprise');
    	$enterprsies = collection($enterprise->field('id,fullName')->order('id')->select())->toArray();
    	$enterprsies[] = array_unshift($enterprsies, ['id'=>0 , 'fullName'=>'管委会']);
    	return json(['code'=>1,'msg'=>'企业列表','data'=>$enterprsies]);
    }
}